package com.naxian.controller.search;

import com.github.pagehelper.PageInfo;
import com.naxian.pojo.Company;
import com.naxian.pojo.Recruit;
import com.naxian.service.Impl.SearchInfoServiceImpl;
import com.naxian.service.company.CompanyService;

import com.naxian.service.recruit.RecruitService;
import com.naxian.service.search.SearchCenterService;
import com.naxian.service.searchInfo.SearchInfoService;
import com.naxian.util.JsonResult;
import com.naxian.vo.SearchInfoDTO;
import com.naxian.vo.SearchInfoResultDTO;
import com.naxian.vo.search.SearchCenterVo;

import com.naxian.vo.searchInfo.SearchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.management.relation.RelationService;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SearchCenterController {
    @Autowired
    private SearchCenterService searchCenterService;
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SearchInfoService searchInfoService;

    //根据key值获得公司和招聘信息
    @GetMapping("recruitList/select")
    public ModelAndView SearchCenter(HttpServletRequest request) {
        //1.获得url后面的页码
        //获得url后面的key
        String pageNumKey = request.getParameter("selectkey");
        String pageNumStr = request.getParameter("page");
        System.out.println("searchCenter controller pageNum:" + pageNumStr);
        Integer pageNum = null;
        //2. 页码数据类型转换
        if (pageNumStr == null)
            pageNum = 1;
        else
            pageNum = Integer.valueOf(pageNumStr);
        //service  调用
        PageInfo<SearchCenterVo> pageInfo = searchCenterService.selectBykey(pageNumKey, pageNum, 100);
        ModelAndView mav = new ModelAndView();
        mav.addObject("searchCenter", pageInfo);
        mav.addObject("searchCenterList", pageInfo.getList());
        mav.addObject("searchCenterpageNumkey", pageNumKey);
        mav.setViewName("indexAndSearch/searchCenter");
        return mav;
    }

    //根据简历名（职位名）获得招聘和公司信息
    @GetMapping("recruitList/selectByRecName")
    public ModelAndView SearchByRecName(HttpServletRequest request) {
        //1.获得url后面的页码
        //获得url后面的key
        String pageNumKey = request.getParameter("selectRecName");
        String pageNumStr = request.getParameter("page");
        System.out.println("searchCenter controller pageNum:" + pageNumStr);
        Integer pageNum = null;
        //2. 页码数据类型转换
        if (pageNumStr == null)
            pageNum = 1;
        else
            pageNum = Integer.valueOf(pageNumStr);
        //service  调用
        PageInfo<SearchCenterVo> pageInfo = searchCenterService.selectByRecName(pageNumKey, pageNum, 100);
        ModelAndView mav = new ModelAndView();
        mav.addObject("searchCenter", pageInfo);
        mav.addObject("searchCenterList", pageInfo.getList());
        mav.addObject("searchCenterpageNumkey", pageNumKey);
        mav.setViewName("indexAndSearch/searchCenter");
        return mav;
    }

    //根据多条件查询
    @PostMapping("recruitList/selectExample")
    @ResponseBody
    public JsonResult selectByExample(@RequestParam(defaultValue = "1", required = false, value = "pageNo") Integer pageNo,
                                      @RequestParam("salary") String salary, @RequestParam("companyType") String companyType,
                                      @RequestParam("companySize") String companySize, @RequestParam("education") String education) {


        SearchInfo searchInfo = new SearchInfo();
        Company company = new Company();
        Recruit recruit = new Recruit();
        List<Company> companyList = new ArrayList<>();
        List<Recruit> recruitList = new ArrayList<>();
        company.setCompanyType(companyType);
        company.setCompanySize(companySize);
        recruit.setRecJobSalary(salary);
        recruit.setEducation(education);

        companyList.add(company);
        recruitList.add(recruit);
        searchInfo.setCompanyList(companyList);
        searchInfo.setRecruitList(recruitList);

        PageInfo<SearchInfo> pageInfo = searchInfoService.getByInfo(pageNo, 5);


        if (pageInfo.getList().size() > 0)
            return new JsonResult(0, null, pageInfo);
        else
            return new JsonResult(1, null, null);

    }

    //根据多条件查询根据多条件查询根据多条件查询根据多条件查询根据多条件查询根据多条件查询根据多条件查询
    @PostMapping("recruitList/selectStyle")
    @ResponseBody

    public JsonResult selectByStyle(@RequestParam(defaultValue = "1", value = "pageNo") Integer pageNo, SearchInfoResultDTO searchInfoResultDTO) {
        System.out.println(searchInfoResultDTO);
        PageInfo<SearchInfoDTO> pageInfo = searchInfoService.query(pageNo, searchInfoResultDTO);
        System.out.println(pageInfo);
        return new JsonResult(200, "成功", pageInfo);
    }



}